<template>
  <div class="grid-icon">
    <div class="container">
      <div
        class="item"
        v-for="(item, index) in list.slice(0, column * row)"
        :key="item.name"
        @click="handleClick(item, index)"
        :style="{ width: itemWidth }"
      >
        <div class="icon">
          <img :src="item.icon" />
        </div>
        <div class="name">{{ item.name }}</div>
        <div class="brief" v-if="item.brief">{{ item.brief }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "GridIcon",
  props: {
    // 数据
    list: Array,
    // 每行显示几个元素
    column: {
      type: Number,
      default: 5,
    },
    // 显示几行
    row: {
      tppe: Number,
      default: 3,
    },
  },
  computed: {
    itemWidth() {
      let { list, column } = this
      let listLen = list.length
      
      if(listLen < column) {
        return 100 / listLen + `%`;
      }else {
        return 100 / column + `%`;
      }
    },
  },
  data() {
    return {};
  },
  methods: {
    handleClick(item, index) {
      this.$emit("click", { item, index });
    },
  },
};
</script>

<style lang="scss" scoped>
$item-bottom: 10px;

.grid-icon {
  background: #fff;
  overflow: hidden;

  .container {
    display: flex;
    flex-wrap: wrap;
    margin-top: -$item-bottom;

    .item {
      text-align: center;
      box-sizing: border-box;
      margin-top: $item-bottom;

      .icon {
        width: 32px;
        height: 32px;
        margin: auto;

        img {
          width: 100%;
        }
      }

      .name {
        color: #333;
        font-size: 13px;
        margin-top: 8px;
        line-height: 1;
      }

      .brief {
        color: #888;
        font-size: 10px;
        margin-top: 5px;
        line-height: 1;
      }
    }
  }
}
</style>
